package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"tie/api/register"
	"tie/initlist/mysql"
	"tie/inits/model"
	"tie/pkg"
)

func Comment(r *gin.Context) {
	var req register.Comment
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "参数失败",
			Data:    nil,
		})
		return
	}
	tr := mysql.DB.Begin()
	comment := model.Comment{
		TieId:   req.TieId,
		Comment: req.Comment,
	}
	tr.Commit()
	mysql.DB.Debug().Create(&comment)
	r.JSON(200, &Res{
		Code:    200,
		Message: "评论成功",
		Data:    comment,
	})
}

func CommentList(r *gin.Context) {
	var req register.Comment
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "参数失败",
			Data:    nil,
		})
		return
	}
	var all []model.Comment
	mysql.DB.Debug().Find(&all)
	rabbitmq := pkg.NewRabbitMQSimple("yyq")
	rabbitmq.PublishSimple("Hello kuteng222!")
	fmt.Println("发送成功！")
	r.JSON(200, &Res{
		Code:    200,
		Message: "评论展示成功",
		Data:    all,
	})
}

func CommentDel(r *gin.Context) {
	var req register.Comment
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "参数失败",
			Data:    nil,
		})
		return
	}

	var comment model.Comment
	err = mysql.DB.Debug().Where("id = ?", comment.ID).Delete(&model.Comment{}).Error
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "删除失败",
			Data:    nil,
		})
		return
	}
	rabbitmq := pkg.NewRabbitMQSimple("yyq")
	rabbitmq.PublishSimple("Hello kuteng222!")
	fmt.Println("发送成功！")
	r.JSON(200, &Res{
		Code:    200,
		Message: "评论删除成功",
		Data:    comment.ID,
	})
}
